using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;



namespace SsbTransportTestSuite
{
    class DBUtils
    {
        internal static void AddServiceOrder(SqlConnection cn,Guid cgid, SsbTransportTestSuite.Services.Order order)
        {
            SqlCommand cmd = new SqlCommand("INSERT INTO ServiceOrders (CGID, OrderId, CustomerName, OrderStatus) VALUES (@cgid, @orderid, @customername, @orderstatus)",cn);
            SqlParameter pcgid=cmd.Parameters.Add("cgid",SqlDbType.UniqueIdentifier);
            pcgid.Value=cgid;
            SqlParameter porderid = cmd.Parameters.Add("orderid", SqlDbType.UniqueIdentifier);
            porderid.Value = order.OrderId;
            SqlParameter pcustomername = cmd.Parameters.Add("customername", SqlDbType.VarChar);
            pcustomername.Value = (object)order.CustomerName??DBNull.Value;

            SqlParameter porderstatus = cmd.Parameters.Add("orderstatus", SqlDbType.Int);
            porderstatus.Value = SsbTransportTestSuite.Services.OrderStatus.New;
            cmd.ExecuteNonQuery();
        }
        internal static void AddServiceOrderItem(SqlConnection cn, Guid cgid, SsbTransportTestSuite.Services.OrderItem item)
        {
            SqlCommand cmd = new SqlCommand("SELECT OrderId from ServiceOrders WHERE CGID=@cgid", cn);
            SqlParameter pcgid = cmd.Parameters.Add("cgid", SqlDbType.UniqueIdentifier);
            pcgid.Value = cgid;
            Guid orderid=(Guid)cmd.ExecuteScalar();
            
            cmd = new SqlCommand("INSERT INTO ServiceOrderDetails (OrderId, ItemDescription, Quantity, Amount) VALUES (@orderid, @descr, @quant, @amt)", cn);
            SqlParameter porderid = cmd.Parameters.Add("orderid", SqlDbType.UniqueIdentifier);
            porderid.Value = orderid;
            SqlParameter pdescr = cmd.Parameters.Add("descr", SqlDbType.VarChar);
            pdescr.Value = (object)item.ItemDescription??DBNull.Value;
            SqlParameter pquant = cmd.Parameters.Add("quant", SqlDbType.Int);
            pquant.Value = item.Quantity;
            SqlParameter pamt = cmd.Parameters.Add("amt", SqlDbType.Decimal);
            pamt.Value = item.Amount;
            cmd.ExecuteNonQuery();
        }
        internal static void UpdateServiceOrder(SqlConnection cn, Guid cgid, SsbTransportTestSuite.Services.OrderStatus orderstatus)
        {
            SqlCommand cmd = new SqlCommand("UPDATE ServiceOrders SET OrderStatus=@orderstatus WHERE CGID=@cgid", cn);
            SqlParameter pcgid = cmd.Parameters.Add("cgid", SqlDbType.UniqueIdentifier);
            pcgid.Value=cgid;
            SqlParameter porderstatus = cmd.Parameters.Add("orderstatus", SqlDbType.Int);
            porderstatus.Value=orderstatus;
            cmd.ExecuteNonQuery();

        }
        internal static void UpdateClientOrderStatus(SqlConnection cn, Guid cid, SsbTransportTestSuite.Clients.OrderStatus orderstatus)
        {
            SqlCommand cmd = new SqlCommand("UPDATE ClientOrders SET OrderStatus=@orderstatus WHERE CID=@cid", cn);
            SqlParameter pcid = cmd.Parameters.Add("cid", SqlDbType.UniqueIdentifier);
            pcid.Value = cid;
            SqlParameter porderstatus = cmd.Parameters.Add("orderstatus", SqlDbType.Int);
            porderstatus.Value = orderstatus;
            cmd.ExecuteNonQuery();
        }
        internal static void AddClientOrder(SqlConnection cn, Guid cid, Guid orderid)
        {
            SqlCommand cmd = new SqlCommand("INSERT INTO ClientOrders (CID, OrderId, OrderStatus) VALUES (@cid, @orderid, @orderstatus)", cn);
            SqlParameter pcid = cmd.Parameters.Add("cid", SqlDbType.UniqueIdentifier);
            pcid.Value = cid;
            SqlParameter porderid = cmd.Parameters.Add("orderid", SqlDbType.UniqueIdentifier);
            porderid.Value = orderid;
            SqlParameter porderstatus = cmd.Parameters.Add("orderstatus", SqlDbType.Int);
            porderstatus.Value = SsbTransportTestSuite.Services.OrderStatus.New;
            cmd.ExecuteNonQuery();
        }
    }
}
